<?php

namespace app\xwam\controller\user;

/**
 * 用户控制器（包含管理员和座席）
 *
 * @version 1.0 2014-6-13> sutroon Added.
 */
class Admin extends \app\xwam\controller\AppbaseController {

    protected $adminModel;

    protected function initialize() {
        parent::initialize();
        $this->adminModel = new \app\xwam\model\AdminModel();
    }

    function parseSearchInfo() {
        $search = $this->request->param();
        $search['department_id'] = $this->request->param('department_id/d');
        $where = [];
        if (!empty($search['word'])) {
            $word = $search['word'];
            $where[] = ['user_name|real_name|mobile', 'like', "%$word%"];
        }
        if (intval($search['department_id']) > 0) {
            $where[] = ['department_id', '=', $search['department_id']];
        }
        return array('search' => $search, 'where' => $where);
    }

    public function index() {
        $searchInfo = $this->parseSearchInfo();
        $list = db(\app\xwam\model\AdminModel::TABLE)->where($searchInfo['where'])->order('id desc')->paginate();
        $assign = [
            'search' => $searchInfo['search'],
            'list' => $list,
        ];
        return view('', $assign);
    }

    /**
     * 导出
     */
    public function export() {
        $searchInfo = $this->parseSearchData();
        $list = db(\app\xwam\model\AdminModel::TABLE)->where($searchInfo['where'])->select();
        if (!$list) {
            $this->error('找不到符合条件的记录!');
        }
        xsfn_excel_export($list->toArray());
    }

    /**
     * 编辑个人资料
     * @version 1.0 2015-11-11 SoChishun Added.
     */
    function profile() {
        $id=$this->loginData['id'];
        $data = $id ? db('user_admin')->find($id) : array();
        return view('', ['data' => $data]);
    }

    /**
     * 保存个人资料
     * @version 1.0 2016-3-11 SoChishun Added.
     */
    function save_profile() {
        $mAdmin = new \app\xwam\model\AdminModel();
        $result = $mAdmin->saveRecord($this->loginData['id']);
        $this->dialogClose($result['status'], $result['info']);
    }

    public function edit($id = 0) {
        $data = $id ? db('user_admin')->find($id) : array();
        $assign = [
            'data' => $data,
            'meta_title' => $id ? '编辑 ' . $data['user_name'] : '新增管理员'
        ];
        return view('', $assign);
    }

    public function save_edit() {
        $data = $this->request->param();
        $result = $this->adminModel->saveRecord($data);
        $this->dialogJump($result);
    }

    public function delete($id = '') {
        if (!$id) {
            return json(xsfn_message(false, '编号丢失!'));
        }
        if ($id == $this->loginData['id']) {
            return json(xsfn_message(false, '不能删除自己!'));
        }
        $result = db('user_admin')->where(['id' => ['in', $id]])->delete();
        return json(xsfn_message($result > 0 ? true : false));
    }

    public function password() {
        return view('password_edit', ['id' => $this->loginData['id']]);
    }

    public function do_password_save() {
        $result = \app\xwam\model\UserModel::savePassword('user_admin');
        $this->dialogClose($result['status'], $result['info']);
    }

    // 更改用户状态
    public function set_value($id, $value, $name = 'status') {
        $result = db('user_admin')->where(array('id' => $id))->setField(array($name => $status));
        return json(xsfn_message($result > 0));
    }

}
